<?php

namespace app\zhongcan\controller;

use app\zhongcan\model\NavigateModel;
use cmf\controller\AdminBaseController;

class NavigateController extends AdminBaseController
{
    // 显示页面
    public function index(){
        // 获取搜索的字段
        $key = input('key');
        $where = [];
        // 判断是否有值
        if($key){
            $where[] = ['name', 'like', '%' . $key . '%'];
        }
        $list = NavigateModel::where($where)->paginate(10);
        return $this->fetch('index',['list'=>$list]);
        /*$list = NavigateModel::select();
        return $this->fetch('index',['list'=>$list]);*/
    }

    // 添加
    public function add(){
        $list = NavigateModel::select();
        return $this->fetch('add',['list' => $list]);
    }
    // 推送添加
    public function addPost(){
        //获取数据
        $data = input('post.');
        $res = NavigateModel::create($data);
        // 入库并判断是否成功
        if ($res){
            $this->success('添加成功','Navigate/index');
        } else {
            $this->error('添加失败');
        }
    }

    // 编辑
    public function edit(){
        $id = input('id');
        $list = NavigateModel::find($id);
        return $this->fetch('edit',['list' => $list]);
    }

    public function editPost(){
        // 获取数据
        $data = input('post.');
        // 入库并判断成功
        $list = NavigateModel::update($data, ['id'=>$data['id']]);
        if($list){
            $this->success('修改成功','Navigate/index');
        }else{
            $this->error('修改失败');
        }
    }

    // 删除
    public function delete(){
        // 查询数据并判断数据是否存在
        $id = input('id/d','');
        $isExist = NavigateModel::find($id);
        if(!$isExist){
            $this->error('删除的数据不存在');
        }

        // 删除数据并判断是否成功
        $del = NavigateModel::destroy($id);
        if($del){
            $this->success('删除成功','Navigate/index');
        }else{
            $this->error('删除失败');
        }
    }
}